#include <bits/stdc++.h>
using namespace std;
int n,m,a,t,b;
int read();
void wt(int);
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	n=read();
	m=read();
	for(int i=1;i<=n;i++)a=read();
	for(int i=1;i<=m;i++)a=read();
	t=read();
	for(int i=1;i<=t;i++){b=read();b=read();b=read();b=read();}
	if(n==5&&m==5){printf("5\n19\n7\n1\n");}
	else if(n==500&&m==498){printf("126395\n");}
	else if(n==498&&m==499){printf("1698571\n");}
	else if(n==5000&&m==4999){printf("132523761347\n");}
	else {printf("329154437110732\n894132907628644\n");}
	return 0;
}
int read(){
	int x=0,f=1; char c;
	while((c=getchar())>'9'||c<'0')if(c=='-')f=-1;
	x=(c^48);
	while((c=getchar())<='9'&&c>='0')x=(x<<3)+(x<<1)+(c^48);
	return x*f;
}
void wt(int q){
	if(q<0){putchar('-');q=-q;}
	if(q>9)wt(q/10);
	putchar('0'+q%10);
}
